package cn.edu.jxau.core.service;

import cn.edu.jxau.core.exception.ServiceException;
import cn.edu.jxau.core.util.upload.FileUpload;
import cn.edu.jxau.framework.dao.core.Session;
import cn.edu.jxau.framework.dao.core.sqlSessionFactory;
import cn.edu.jxau.framework.dao.exception.ConfigurationException;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:光谱管理业务
 * @author：SongQingZheng
 * @create 2017 08 24 9:37
 */
public class SpectrumManageService extends ServiceSupport {

    private final String SPECTRUMMSG="共查询到0条记录";
    //日志信息
    private Logger logger =  LoggerFactory.getLogger(SpectrumManageService.class);

    public SpectrumManageService() {
    }

    /**
     * @param view   service.xml中的name和视图对应
     * @param params request入口的参数数组
     * @param data   返回给Servlet的出口参数
     * @throws ServiceException
     */
    public SpectrumManageService(String view, Map<String, Object[]> params, Map<String, Object> data) throws ServiceException {

        super(view, params, data);
        execute(view, params, data); // 执行业务
    }

    /**
     * 查询所有光谱
     * @param params
     * @param data
     * @return
     */
    protected void listAllSpectrum(Map<String, Object[]> params, Map<String, Object> data) {

        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据

        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        String fileID=getParamStringValue(params, "fileId",0); //获取文件夹的id
        Matcher isNum = pattern.matcher(fileID);
        //判断field是否为空或是否是数字
        if(null!=fileID&&isNum.matches()) {
            int ID=Integer.parseInt(fileID);
            param.put("fileID", ID);
        }else{
           return ;
        }
        Session session = null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("querySpectrum", param, datas);
            System.out.println(datas);
            if(null==datas||datas.isEmpty()){
                data.put("status","0");
                data.put("isempty",SPECTRUMMSG);
                data.put("result", SUCCESS);
            }else{
                data.put("status","1");
                data.put("spectrumdatas",datas);
                data.put("result",SUCCESS);
            }
            session.releaseConnection();//释放会话工厂
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示光谱详细信息
     * @param params
     * @param data
     */
    protected void showSpectrumDetail(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据

        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        String spectrumId=getParamStringValue(params, "spectrumId",0); //获取光谱的id
        Matcher isNum = pattern.matcher(spectrumId);
        //判断field是否为空或是否是数字
        if(null!=spectrumId&&isNum.matches()) {
            int ID=Integer.parseInt(spectrumId);
            param.put("spectrumId", ID);
        }else{
            return ;
        }
        Session session=null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("querySpectrumDetail", param, datas);
            if(null==datas||datas.isEmpty()){
                return;
            }else{

                    data.put("spectrumDetail", datas);

                    data.put("result",SUCCESS);
            }
            session.releaseConnection();//释放会话工厂
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除光谱
     * @param params
     * @param data
     */
    protected  void deleteSpectrum(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的数据
        Pattern pattern = Pattern.compile("[0-9]*");    //正则判断字符串是否是数字
        String spectrumID=getParamStringValue(params,"spectrumId",0);
        Matcher isNum = pattern.matcher(spectrumID);
        if(null!=spectrumID&&isNum.matches()){
            int ID=Integer.parseInt(spectrumID);
            param.put("spectrumID",ID);
            param.put("delete",1);
        }else{
            return;
        }
        Session session=null;
        try {
            session=sqlSessionFactory.getInstance().getSession();
            boolean flag=session.execute("updateSpectrum",param,datas);
            if(flag){
                data.put("successDelete","删除成功");
                data.put("result",SUCCESS);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }finally {
            session.releaseConnection();
        }
    }

}
